<template>
  <div class="mainor">
    <div class="main_spac1" style="padding-bottom: 5px">
      <el-form
        :inline="true"
        :rules="rules"
        :model="monitorForm"
        class="demo-form-inline mainpu_form marginTop0"
        ref="userListFrom"
        label-width="100px"
      >
        <div class="main_spac1 padding0" style="padding-bottom: 5px">
          <div class="main_white padding10">
            <div class="main_crumbs boxdq">
              <div class="main_crumbs_l">
                <a href="javascript:;" @click="$router.back(-1)">
                  <i class="el-icon-arrow-down"></i>返回
                </a>
                <em>|</em>
                <span>添加退货单</span>
              </div>
              <div class="main_crumbs_r boxnr" style="margin-top: -5px">
                <el-checkbox-group v-model="monitorForm.form.type">
                  <el-checkbox
                    :label="1"
                    class="marginRight20"
                    @change="checkboxGroup(1)"
                    >按商品</el-checkbox
                  >
                  <el-checkbox
                    :label="2"
                    class="marginRight20"
                    @change="checkboxGroup(2)"
                    >按采购单</el-checkbox
                  >
                </el-checkbox-group>
              </div>
            </div>
            <div class="marginTop15">
              <el-form-item label="采购方式" prop="form.mode">
                <el-select
                  v-model.trim="monitorForm.form.mode"
                  placeholder="采购方式"
                  class="wid220"
                  @change="
                    (monitorForm.form.buyer_id = null),
                      (monitorForm.form.supplier_id = null)
                  "
                >
                  <el-option
                    v-for="(modeList, index) in info.modeList"
                    :key="index"
                    :label="modeList"
                    :value="parseInt(index)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label=""
                prop="form.buyer_id"
                v-if="monitorForm.form.mode == 1"
                :rules="rules.buyer_id"
              >
                <el-select
                  v-model.trim="monitorForm.form.buyer_id"
                  ref="company"
                  placeholder="采购员"
                  class="wid220"
                >
                  <el-option
                    v-for="(buyer, index) in info.buyer"
                    :key="index"
                    :label="buyer"
                    :value="parseInt(index)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label=""
                prop="form.supplier_id"
                v-if="monitorForm.form.mode > 1"
                ref="company"
                :rules="rules.supplier_id"
              >
                <el-select
                  v-model.trim="monitorForm.form.supplier_id"
                  placeholder="供应商"
                  class="wid220"
                >
                  <el-option
                    v-for="(supplier, index) in info.supplier"
                    :key="index"
                    :label="supplier"
                    :value="parseInt(index)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <div class="clearfix"></div>
              <div class="marginTop5">
                <el-form-item
                  label="备注："
                  prop="form.remark"
                  class="marginRight0 marginTop10 remark_in"
                >
                  <el-input
                    v-model.trim="monitorForm.form.remark"
                    placeholder="备注"
                    clearable
                  ></el-input>
                </el-form-item>
              </div>
            </div>
          </div>
        </div>
        <div class="main_white padding10 marginTop10 marginBottom10">
          <div>
            <div class="main_white">
              <div v-if="monitorForm.form.type[0] == 1">
                <div class="mainor_sp boxha">
                  <ul class="box">
                    <li class="box">
                      <span> <em>*</em>商品名称： </span>
                      <el-autocomplete
                        v-model="formsp.value"
                        :trigger-on-focus="true"
                        ref="searchSelect"
                        @clear="$refs.searchSelect.$children[0].blur()"
                        class="wid240 main_comm"
                        :fetch-suggestions="
                          (queryString, cb) => {
                            querySearchAsync(queryString, cb);
                          }
                        "
                        placeholder="请输入内容"
                        clearable
                        :highlight-first-item="true"
                        value-key="name"
                        @select="
                          (item) => {
                            handleSelect(item);
                          }
                        "
                      ></el-autocomplete>
                    </li>
                    <li class="box">
                      <span> <em>*</em>数量： </span>
                      <el-input
                        v-model.trim="formsp.num"
                        ref="focusw2"
                        placeholder="数量"
                        clearable
                        class="wid140"
                        @keyup.enter.native="mainor_sp"
                      ></el-input>
                    </li>
                  </ul>
                  <div class="marginBottom10">
                    <el-button type="primary" @click="mainor_qr"
                      >确认(回车)</el-button
                    >
                    <el-button
                      class="reset marginLeft5"
                      type="info"
                      @click="reset"
                      >重置</el-button
                    >
                  </div>
                </div>
                <div class="main_table marginTop0" style="min-height: 370px">
                  <el-table
                    ref="multipleTable"
                    :data="monitorForm.list"
                    element-loading-text="加载中"
                    fit
                    border
                    highlight-current-row
                    key="multipleTable"
                  >
                    <el-table-column label="序号">
                      <template slot-scope="scope">{{
                        scope.$index + 1
                      }}</template>
                    </el-table-column>
                    <el-table-column label="商品名称" prop="name" width="150">
                      <template slot-scope="scope"
                        >{{ scope.row.name }}【{{
                          scope.row.spu_name
                        }}】</template
                      >
                    </el-table-column>
                    <el-table-column label="单位" prop="unit" />
                    <el-table-column
                      label="库存"
                      prop="purchaseReturn_inventory"
                    />
                    <el-table-column
                      label="数量"
                      prop="price"
                      width="150"
                      class-name="main_hei"
                    >
                      <template slot-scope="scope">
                        <el-form-item
                          :prop="'list.' + scope.$index + '.num'"
                          :rules="rules.num"
                          label-width="10px"
                          label=" "
                        >
                          <el-input
                            v-model.trim="scope.row.num"
                            :ref="'num' + scope.row.goods_spu_id"
                            placeholder="数量"
                            class="wid110"
                          ></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                    <el-table-column
                      label="单价"
                      prop="price"
                      width="150"
                      class-name="main_hei"
                    >
                      <template slot-scope="scope">
                        <el-form-item label-width="0" label="">
                          <el-input
                            v-model.trim="scope.row.price"
                            placeholder="单价"
                            class="wid110"
                          ></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                    <el-table-column label="小计" prop="price" width="150">
                      <template slot-scope="scpoe">{{
                        parseFloat(scpoe.row.num) * parseFloat(scpoe.row.price)
                      }}</template>
                    </el-table-column>
                    <el-table-column
                      label="备注"
                      prop="remark"
                      width="350"
                      class-name="main_hei"
                    >
                      <template slot-scope="scope">
                        <el-form-item label-width="0" label="">
                          <el-input
                            v-model.trim="scope.row.remark"
                            placeholder="备注"
                            class="wid240"
                          ></el-input>
                        </el-form-item>
                        <el-link
                          type="danger"
                          @click="scdelete(scope.$index)"
                          class="paddingTop20"
                          >删除</el-link
                        >
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </div>
              <div v-else>
                <div class="mainor_sp boxha">
                  <div class="marginBottom10">
                    <el-button type="primary" @click="purchaseClick"
                      >选择采购单</el-button
                    >
                  </div>
                </div>
                <div class="main_table marginTop0" style="min-height: 370px">
                  <el-table
                    ref="multipleTable2"
                    :data="monitorForm.list_pu"
                    element-loading-text="加载中"
                    fit
                    border
                    highlight-current-row
                    :row-class-name="getRowClass"
                    key="multipleTable2"
                  >
                    <el-table-column label="序号">
                      <template slot-scope="scope">{{
                        scope.$index + 1
                      }}</template>
                    </el-table-column>
                    <el-table-column label="商品名称" prop="name" width="150">
                      <template slot-scope="scope"
                        >{{ scope.row.name }}【{{
                          scope.row.spu_name
                        }}】</template
                      >
                    </el-table-column>
                    <el-table-column label="单位" prop="unit" />
                    <el-table-column
                      label="实时库存"
                      width="120"
                      prop="goods_spu.inventory.num"
                    />
                    <el-table-column
                      label="实采数量"
                      width="120"
                      prop="purchased_num"
                    />
                    <el-table-column
                      label="采购单价"
                      width="120"
                      prop="price"
                    />
                    <el-table-column
                      label="已退数量"
                      width="120"
                      prop="return_num"
                    />
                    <el-table-column
                      label="退货数量"
                      width="160"
                      prop="return_inventory"
                      class-name="main_hei"
                    >
                      <template slot-scope="scope">
                        <div v-if="scope.row.return_status == 2">
                          {{ scope.row.return_inventory }}
                        </div>
                        <el-form-item
                          :prop="
                            'list_pu.' + scope.$index + '.return_inventory'
                          "
                          v-else
                          :rules="rules.return_inventory"
                          label-width="10px"
                          label=" "
                        >
                          <el-input
                            v-model.trim="scope.row.return_inventory"
                            placeholder="退货数量"
                            class="wid110"
                            @change="numberChange($event, scope.$index)"
                          ></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                    <el-table-column
                      label="退货单价"
                      width="160"
                      prop="price"
                      class-name="main_hei"
                    >
                      <template slot-scope="scope">
                        <div v-if="scope.row.return_status == 2">
                           {{ scope.row.price }}
                        </div>
                        <el-form-item
                          :prop="'list_pu.' + scope.$index + '.price'"
                          v-else
                          :rules="rules.price"
                          label-width="10px"
                          label=" "
                        >
                          <el-input
                            v-model.trim="scope.row.price"
                            placeholder="退货单价"
                            class="wid110"
                          ></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                    <el-table-column label="退货金额" width="120">
                      <template slot-scope="scope">
                        {{
                          parseFloat(scope.row.return_inventory) *
                          parseFloat(scope.row.price)
                            ? (
                                parseFloat(scope.row.return_inventory) *
                                parseFloat(scope.row.price)
                              ).toFixed(2)
                            : 0
                        }}
                      </template>
                    </el-table-column>
                    <el-table-column
                      label="备注"
                      width="350"
                      prop="return_remark"
                      class-name="main_hei"
                    >
                      <template slot-scope="scope">
                        <div v-if="scope.row.return_status == 2">
                          {{ scope.row.return_remark }}
                        </div>
                        <div v-else>
                          <el-form-item label-width="0" label="">
                            <el-input
                              v-model.trim="scope.row.return_remark"
                              placeholder="备注"
                              class="wid240"
                            ></el-input>
                          </el-form-item>
                          <el-link
                            type="danger"
                            @click="deletClick(scope.$index, scope.row.id)"
                            class="paddingTop20"
                            >删除</el-link
                          >
                        </div>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </div>

              <div class="newConBox_p200 paddingBottom10" id="newConBox">
                <div class="newConBox_h32">
                  <div
                    :class="[
                      'mainor_pr boxdq newConBox ',
                      { on: searchBarFixed },
                    ]"
                  >
                    <div class="mainor_prl">
                      <el-button
                        type="primary"
                        @click="preservation(0)"
                        v-repeatClick
                        >保存</el-button
                      >
                      <el-button @click="$router.back(-1)">取消</el-button>
                    </div>
                    <div class="mainor_prr">
                      <span>合计金额：¥{{ total }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-form>

      <!-- 采购单 -->
      <el-dialog
        title="请选择采购单"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="close"
        :visible.sync="purchaseed"
        class="main_dialog dialog900"
      >
        <div class="mainor_form">
          <el-form
            :inline="true"
            :model="form_sh"
            class="demo-form-inline"
            ref="userListFrom1"
            label-width="1px"
          >
            <el-form-item label=" " prop="order_sn">
              <el-input
                v-model.trim="form_sh.order_sn"
                placeholder="采购单号"
                clearable
                class="wid180"
              ></el-input>
            </el-form-item>
            <el-form-item label="" prop="createTime" class="marginLeft10">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="$refs['userListFrom1'].resetFields(), (createTime = [])"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table">
          <el-table
            ref="multipleTable1"
            v-loading="listLoading"
            :data="list_sh.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
          >
            <el-table-column
              label="采购单号"
              prop="order_sn"
              width="140"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="采购金额"
              prop="money"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="采购时间"
              prop="order_sn"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                {{ parseTime(scope.row.purchase_time, "{y}-{m}-{d}") }}
              </template>
            </el-table-column>
            <el-table-column
              label="状态"
              prop="order_sn"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                {{ list_sh.status[scope.row.status] }}
              </template>
            </el-table-column>
            <el-table-column
              label="已全退"
              prop="order_sn"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                {{
                  scope.row.return_status == 1
                    ? "部分退"
                    : scope.row.return_status == 0
                    ? "未退"
                    : "全退"
                }}
              </template>
            </el-table-column>
            <el-table-column
              label="管理操作"
              align="center"
              width="300"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link
                  type="success"
                  @click="purchasebfClick(scope.$index)"
                  v-if="scope.row.return_status != 2"
                  >退货</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current_sh.page"
              :page-sizes="[10, 30, 50, 100]"
              :page-size="limit_sh"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount_sh"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
const shuju = {
  list: {
    name: "",
    goods_id: "",
    goods_spu_id: "",
    spu_name: "",
    unit: "",
    purchaseReturn_inventory: "",
    price: "",
    num: "",
    remark: "",
    show: false,
  },
  form: {
    mode: 1,
    buyer_id: null,
    supplier_id: null,
    pid: "",
    remark: "",
  },
  formsp: {
    name: [],
    girard: null,
    num: "",
    value: "",
  },
};

import {
  purchaseReturn_base,
  purchaseReturn_add,
  purchaseReturn_getInventory,
  purchaseReturn_getPurchase,
  purchaseReturn_selectPurchase,
} from "@/api/purchasing/purchasingReturn";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import rules from "@/rules/purchasing/purchasingReturn_add";
import { goods_getGoodsSpu } from "@/api/currency";
import dateOption from "@/options/dateOption";
import { parseTime } from "@/utils/index";

export default {
  computed: {
    total() {
      if (this.monitorForm.form.type[0] == 1) {
        var sz = 0;
        this.monitorForm.list.forEach((e) => {
          sz += parseFloat(e.num * e.price);
        });
      } else {
        var sz = 0;
        this.monitorForm.list_pu.forEach((e) => {
          sz += parseFloat(e.return_inventory * e.price);
        });
      }
      return sz ? sz.toFixed(2) : 0;
    },
  },
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      // 采购单
      purchaseed: false,
      form_sh: {
        mode: 1,
        buyer_id: null,
        supplier_id: null,
        order_sn: "",
        start_time: "",
        end_time: "",
      },
      createTime: [],
      pickerOptions: dateOption,
      current_sh: {
        page: 1,
        per_page: 10,
      },
      limit_sh: 10,
      totalCount_sh: 1,
      list_sh: {
        list: {
          data: [],
        },
      },
      listLoading: false,
      // 验证
      rules,
      // 列表数据
      monitorForm: {
        form: {
          mode: 1,
          type: [1],
          buyer_id: null,
          supplier_id: null,
          pid: "",
          remark: "",
        },
        list: [],
        list_pu: [],
      },
      inventory: "",
      price: "",
      info: [],
      // 商品搜索
      formsp: {
        name: [],
        girard: null,
        num: "",
        value: "",
      },
      show: true,
      // 监听滚动
      searchBarFixed: false,
    };
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  created() {
    // 信息
    this.purchaseReturn_base();
  },
  methods: {
    // 关闭清空表单
    close() {
      this.createTime = [];
      this.$refs["userListFrom1"].clearValidate();
    },
    // 判断表格是否有子项，无子项不显示展开按钮
    getRowClass(row) {
      if (row.row.return_status==2) {
        return "row_cover";
      }
    },
    // 监听滚动
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop + document.documentElement.clientHeight >
      document.getElementById("newConBox").offsetTop + 300
        ? (this.searchBarFixed = false)
        : (this.searchBarFixed = true);
    },
    // 监听输入
    numberChange(val, index) {
      if (val <= parseFloat(this.monitorForm.list_pu[index].purchased_num)) {
        this.monitorForm.list_pu[index].return_inventory = parseFloat(val);
      } else {
        this.monitorForm.list_pu[index].return_inventory = parseFloat(
          this.monitorForm.list_pu[index].purchased_num
        );
      }
    },
    // 采购单
    purchaseClick() {
      if (
        this.monitorForm.form.buyer_id == null &&
        this.monitorForm.form.supplier_id == null
      ) {
        this.$message.error("请选择采购员或者供应商");
        this.$refs.company.focus();
        return;
      }
      this.purchaseReturn_getPurchase();
      this.purchaseed = true;
    },
    purchasebfClick(index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, {
                id: this.list_sh.list.data[index].id,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.list_sh.list.data[index].id }
      );
      purchaseReturn_selectPurchase(data).then((res) => {
        if (res.errcode == 0) {
          this.purchaseed = false;
          this.monitorForm.form.pid = this.list_sh.list.data[index].id;
          this.monitorForm.list_pu = res.data.list;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 搜索
    getArticleList() {
      this.current_sh.page = 1;
      this.purchaseReturn_getPurchase();
    },
    // 分页
    changeLimit(pageSize) {
      this.current_sh.per_page = pageSize;
      this.purchaseReturn_getPurchase();
    },
    changePage(page) {
      this.current_sh.page = page;
      this.purchaseReturn_getPurchase();
    },
    purchaseReturn_getPurchase() {
      this.listLoading = true;
      let form_sh = JSON.parse(JSON.stringify(this.form_sh));
      form_sh.supplier_id = this.monitorForm.form.supplier_id;
      form_sh.buyer_id = this.monitorForm.form.buyer_id;
      form_sh.mode = this.monitorForm.form.mode;
      form_sh.start_time = this.createTime[0] ? this.createTime[0] : "";
      form_sh.end_time = this.createTime[1] ? this.createTime[1] : "";
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form_sh)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form_sh
      );
      purchaseReturn_getPurchase(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list_sh = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 类型选择
    checkboxGroup(val) {
      this.monitorForm.form.type = [];
      this.monitorForm.form.type.push(val);
    },
    // 商品搜索
    querySearchAsync(queryString, cb) {
      if (
        this.monitorForm.form.buyer_id == null &&
        this.monitorForm.form.supplier_id == null
      ) {
        this.$message.error("请选择采购员或者供应商");
        this.$refs.company.focus();
        cb([]);
        return;
      }
      if (queryString.trim() == "") {
        cb([]);
        return;
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { name: queryString.trim() })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { name: queryString.trim() }
      );
      goods_getGoodsSpu(data).then((res) => {
        if (res.errcode == 0) {
          res.data.goods.map((e) => {
            e.name = e.goods.name + "【" + e.spu_unit.title + "】";
          });
          var results = res.data.goods;
          if (this.$refs.searchSelect) {
            this.$refs.searchSelect.activated = true;
          }
          cb(results);
        } else {
          this.$message.error(res.errmsg);
          cb([]);
        }
      });
    },
    // 商品选择
    handleSelect(item) {
      this.formsp.name = item;
      this.formsp.girard = item.id;
      this.purchaseReturn_getInventory();
    },
    purchaseReturn_getInventory() {
      let formsp = JSON.parse(JSON.stringify(this.formsp));
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, {
                goods_id: formsp.name.goods_id,
                goods_spu_id: formsp.girard,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        {
          goods_id: formsp.name.goods_id,
          goods_spu_id: formsp.girard,
        }
      );
      purchaseReturn_getInventory(data).then((res) => {
        if (res.errcode == 0) {
          this.price = res.data.data.price;
          this.inventory = res.data.data.inventory;
          setTimeout(() => {
            this.$refs.focusw2.focus();
          }, 100);
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 商品选择确认(回车)
    mainor_sp(e) {
      var keyCode = window.event ? e.keyCode : e.which;
      if (keyCode == 13) {
        this.mainor_qr();
      }
    },
    // 点击添加
    mainor_qr() {
      let formsp = JSON.parse(JSON.stringify(this.formsp));
      if (!formsp.name.name) {
        this.$message.error("请填写商品名称");
        this.$refs.searchSelect.focus();
        return;
      } else if (formsp.num == "") {
        this.$message.error("请填写数量");
        this.$refs.focusw2.focus();
        return;
      } else {
        let monitorForm_list = JSON.parse(
          JSON.stringify(this.monitorForm.list)
        );
        let ind = 0;
        if (monitorForm_list.length != 0) {
          monitorForm_list.map((e, index) => {
            if (e.goods_spu_id == formsp.girard) {
              ind = e.goods_spu_id;
              return;
            }
          });
          if (ind == 0) {
            this.handle(formsp);
          } else {
            setTimeout(() => {
              this.$refs["num" + ind].focus();
              this.reset();
              ind = 0;
            }, 100);
          }
        } else {
          this.handle(formsp);
        }
      }
    },
    handle(formsp) {
      let list = JSON.parse(JSON.stringify(shuju.list));
      list.name = formsp.name.goods.name;
      list.goods_id = formsp.name.goods_id;
      list.goods_spu_id = formsp.name.id;
      list.spu_name = formsp.name.spu_name.title;
      list.unit = formsp.name.spu_unit.title;
      list.price = this.price;
      list.purchaseReturn_inventory = this.inventory;
      list.num = formsp.num;
      list.remark = formsp.name.remark;
      this.monitorForm.list.push(list);
      this.reset();
      setTimeout(() => {
        this.$refs.searchSelect.focus();
      }, 100);
    },
    reset() {
      this.formsp = JSON.parse(JSON.stringify(shuju.formsp));
      this.show = false;
      this.$nextTick(() => {
        this.show = true;
      });
    },
    // 删除
    scdelete(index) {
      this.monitorForm.list.splice(index, 1);
    },
    deletClick(index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, {
                id: this.monitorForm.list_pu[index].id,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.monitorForm.list_pu[index].id }
      );
      if (parseFloat(this.monitorForm.list_pu[index].return_inventory) > 0) {
        this.$confirm("已输入退货数量，确定删除吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            purchaseReturn_base(data).then((res) => {
              if (res.errcode == 0) {
                this.monitorForm.list_pu.splice(index, 1);
              } else {
                this.$message.error(res.errmsg);
              }
            });
          })
          .catch(() => {});
      } else {
        this.monitorForm.list_pu.splice(index, 1);
      }
    },
    // 保存
    preservation(type) {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          let form = JSON.parse(JSON.stringify(this.monitorForm.form));
          let sz = JSON.parse(JSON.stringify(this.monitorForm.list));
          let cg = JSON.parse(JSON.stringify(this.monitorForm.list_pu));
          let num = 0;
          if (this.monitorForm.form.type[0] == 1) {
            if (this.monitorForm.list.length == 0) {
              this.$message.error("请至少添加一个商品");
              return;
            }
            sz.map((e) => {
              if (parseFloat(e.num) == 0 || parseFloat(e.price) == 0) {
                num++;
              }
            });
            var list = sz.map((e) => {
              let {
                show,
                spu_name,
                unit,
                purchaseReturn_inventory,
                name,
                ...sz
              } = e;
              return { ...sz };
            });
            let { pid, ...qd } = form;
            form = qd;
          } else {
            if (this.monitorForm.list_pu.length == 0) {
              this.$message.error("请至少添加一个商品");
              return;
            }
            var list = [];
            cg.map((e) => {
              if(e.return_status!=2){
                if (
                  parseFloat(e.return_inventory) == 0 ||
                  parseFloat(e.price) == 0
                ) {
                  num++;
                }
              }
            });
            cg.map((e) => {
              if(e.return_status!=2){
                list.push({
                  purchase_list_id: e.id,
                  goods_id: e.goods_id,
                  goods_spu_id: e.goods_spu_id,
                  num: e.return_inventory,
                  remark: e.return_remark,
                  price: e.price,
                });
              }
            });
          }
          if (num > 0) {
            this.$message.error("请输入数量和单价大于0");
            return;
          }
          form.type = form.type[0];
          form.buyer_id = form.buyer_id ? form.buyer_id : 0;
          form.supplier_id = form.supplier_id ? form.supplier_id : 0;
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name, { status: type })) +
                  apiUrl().appsecret
              ),
            },
            apiUrl().name,
            form,
            { list: JSON.stringify(list), status: type }
          );
          purchaseReturn_add(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.$router.go(-1);
            } else {
              this.$message.error(res.errmsg);
            }
          });
        }
      });
    },
    // 信息
    purchaseReturn_base() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      purchaseReturn_base(data).then((res) => {
        if (res.errcode == 0) {
          this.info = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainor {
  .main_dialog {
    .mainor_form form {
      width: 100%;
    }
    .main_table {
      /deep/.el-table__body-wrapper {
        overflow-y: auto;
        height: 360px;
      }
      /deep/.el-checkbox {
        margin-bottom: 0;
      }
    }
  }

  .mainpu_form {
    /deep/.el-button--default {
      border-color: $menuActiveText;
      color: $menuActiveText;
    }
    /deep/.el-form {
      margin-top: 10px;
      /deep/.el-form-item--mini.el-form-item,
      /deep/.el-form-item--small.el-form-item {
        margin-bottom: 10px;
      }
    }
    .remark_in {
      width: 100%;
      /deep/.el-form-item__content {
        width: 80%;
        .el-input {
          width: 80%;
        }
      }
    }
    .newConBox.on {
      width: calc(100% - 230px);
      padding-left: 0;
    }
    .main_crumbs_us {
      width: 240px;
    }
    .mainor_sp {
      ul {
        margin-right: 20px;
        li {
          margin-bottom: 10px;
          & > span {
            display: inline-block;
            line-height: 32px;
            width: 60px;
            color: $colour_18;
            font-size: $font_14;
            text-align: right;
            em {
              color: $colour_red;
              margin-right: 5px;
            }
          }
          &:first-child {
            & > span {
              width: 85px;
            }
          }
        }
      }
    }
    .main_table {
      /deep/.el-table--small td {
        padding-top: 0;
      }
      /deep/.el-table--small .row_cover td {
        padding-top: 12px;
      }
      /deep/.el-form-item--small.el-form-item {
        margin-bottom: 16px;
        margin-top: 16px;
        margin-right: 0;
      }
    }
    .mainor_pr {
      margin-left: 10px;
      margin-bottom: 10px;
      /deep/.el-button--default {
        border-color: $subActive;
        color: $menuText;
        &:hover {
          border-color: $menuActiveText;
          color: $menuActiveText;
        }
      }
      .mainor_prr {
        font-size: $font_16;
        color: $colour_18;
        span {
          color: #fe3b3b;
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
